<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="股票代码" prop="stockCode">
        <el-input
            v-model="queryParams.stockCode"
            placeholder="请输入股票代码"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="股票名称" prop="stockName">
        <el-input
            v-model="queryParams.stockName"
            placeholder="请输入股票名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易所" prop="exchangeId">
        <el-select v-model="queryParams.exchangeId" placeholder="请选择交易所" clearable style="width: 240px">
          <el-option
              v-for="dict in stock_exchange_id"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Refresh"
            @click="handleIncrementSync"
            v-hasPermi="['stock:info:edit']"
        >增量同步
        </el-button>
        <el-button
            type="warning"
            plain
            icon="Refresh"
            @click="handleFullSync"
            v-hasPermi="['stock:info:edit']"
        >全量同步
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="股票代码" align="center" prop="stockCode"/>
      <el-table-column label="股票名称" align="center" prop="stockName"/>
      <el-table-column label="交易所代码" align="center" prop="exchangeId">
        <template #default="scope">
          <dict-tag :options="stock_exchange_id" :value="scope.row.exchangeId"/>
        </template>
      </el-table-column>
      <el-table-column label="上市日" align="center" prop="ipoDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.ipoDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退市日" align="center" prop="expireDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

  </div>
</template>

<script setup name="Info">
import {listInfo, syncFull, syncIncremental} from "@/api/stock/info";

const {proxy} = getCurrentInstance();
const {stock_exchange_id} = proxy.useDict('stock_exchange_id');

const infoList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stockCode: null,
    stockName: null,
    exchangeId: null,
  }
});

const {queryParams} = toRefs(data);

/** 查询股票基础信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 增量同步 */
function handleIncrementSync() {
  proxy.$modal.confirm('是否确认执行增量同步操作?').then(function () {
    syncIncremental();
    proxy.$modal.msgSuccess("已提交增量同步请求");
  }).catch(() => {});
}

/** 全量同步 */
function handleFullSync() {
  proxy.$modal.confirm('是否确认执行全量同步操作?').then(function () {
    syncFull();
    proxy.$modal.msgSuccess("已提交全量同步请求");
  }).catch(() => {});
}
getList();
</script>